{% extends 'layout/manage.html' %}
{% load static %}
{% block css %}
    <link rel="stylesheet" href="{% static 'plugin/editor.md-master/css/editormd.min.css' %}">
    <style>
        .panel-body {
            padding: 0;
        }

        .title-list {
            border-right: 1px solid #dddddd;
            min-height: 300px;
        }

        .title-list ul {
            padding-left: 15px;
        }

        .title-list ul a {
            display: block;
            padding: 5px 0;
        }

        .content {
            border-left: 1px solid #dddddd;
            min-height: 500px;
            margin-left: -1px;
        }

        .err-msg {
            color: red;
            font-size: 12px;
            position: absolute;
        }

        .panel-default .panel-heading {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }

        .editormd-fullscreen {
            z-index: 1001;
        }
    </style>
{% endblock %}
{% block content %}
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading "><i class="fa fa-book" aria-hidden="true">&nbsp;wiki文档</i>
                <div class="function">
                    <a type="button" href="{% url 'wiki_add' project_id=request.tracer.project.id %}"
                       class="btn btn-success btn-xs">
                        <i class="fa fa-plus" aria-hidden="true"></i> 新建
                    </a>
                </div>
            </div>

            <div class="panel-body">
                <div class="col-sm-3 title-list">
                    <ul id="catalog">
                    </ul>
                </div>
                <div class="col-sm-9 content">
                    <form method="post" novalidate id="form-reg">
                        {% csrf_token %}
                        {% for field in form %}
                            {% if field.name == 'content' %}
                                <div class="form-group">
                                    <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                                    <div id="editor">{{ field }} </div>
                                    <span class="err-msg">{{ field.errors.0 }}</span>
                                </div>
                            {% else %}
                                <div class="form-group">
                                    <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                                    {{ field }}
                                    <span class="err-msg">{{ field.errors.0 }}</span>
                                </div>
                            {% endif %}
                        {% endfor %}
                        <input type="submit" class="btn btn-primary" value="提交">
                    </form>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block js %}
    <script src="{% static 'plugin/editor.md-master/editormd.min.js' %}"></script>
    <script>
        var WIKI_DETAIL_URL = "{% url 'wiki' project_id=request.tracer.project.id %}"
        // 进入界面变发送 ajax 请求，获取后端发来的目录
        $(function () {
            initCataLog();
            initEditorMd();
        });

        /*
       初始化markdown编辑器（textarea转换为编辑器）
        */
        function initEditorMd() {
            editormd('editor', {
                placeholder: "请输入内容",
                height: 300,
                path: "{% static 'plugin/editor.md-master/lib/' %}",
                imageUpload:true, // 开启本地上传
                imageFormats:['jpg','jpeg','gif','png'], //指定上传的图片格式
                imageUploadURL:"{% url 'wiki_upload' project_id=request.tracer.project.id %}", // 上传文件的路径
            })
        }

        function initCataLog() {
            $.ajax({
                url: '{% url 'wiki_catalog' project_id=request.tracer.project.id %}',
                type: 'get',
                dataType: 'JSON',
                success: function (data) {
                    if (data.status) {
                        // 循环获取数据
                        $.each(data.data_list, function (index, item) {
                            // 判断每条数据的 item.parent_id 是否存在，如果没有则是第一级目录,并创建li标签，添加至url中
                            var href = WIKI_DETAIL_URL + "?wiki_id=" + item.id
                            var li = $('<li>').attr('id', 'id_' + item.id).append($('<a>').text(item.title).attr("href", href)).append($('<ul>'))
                            if (!item.parent_id) {
                                // 找到li的父标签url，将其添加进去
                                $('#catalog').append(li);
                            } else {
                                var href = WIKI_DETAIL_URL + "?wiki_id=" + item.id
                                // 如果有parent_id 则表示子文章，根据parent_id 确定父文章的标签id，在找到父标签的儿子的ul,将其添加进去
                                $('#id_' + item.parent_id).children('ul').append(li)
                            }
                        })
                    }
                }
            })
        }
    </script>
{% endblock %}